
import Vue from "vue"
import VueRouter from "vue-router"
import store from "../store"
Vue.use(VueRouter)
const routes = [
    {
        path: "/",
        name: "Index",
        component: () => import("../views/Index.vue"),
        children: [
            {
                path: "/Home",
                name: "Home",
                component: () => import("../views/system/Home.vue")
            },

        ]
    },
    {
        path: "/Login",
        name: "Login",
        component: () => import("../views/Login.vue")
    },
    {
        path: "/Register",
        name: "register",
        component: () => import("../views/Register.vue"),
    },

]
const router = new VueRouter({
    mode: "history",
    routes
})
//路由守卫
router.beforeEach(async (to, from, next) => {

    let hasRoutes = store.state.hasRoutes
    const user = localStorage.getItem("CurUser")
    console.log(hasRoutes)
    let routes = router.options.routes
    console.log(routes)
    if (routes[0].children.length == 1 && hasRoutes != false) {
        hasRoutes = false
        store.commit("changeRoutes", hasRoutes)
    }
    if (user == null && to.path !== "/Login" && to.path !== "/Register") {
        next({ path: "/Login" });
        console.log("未登录")
    } else if (user && !hasRoutes) {
        const menu = await loadMenu(JSON.parse(user))
        
        addRoutes(menu)
        store.commit("changeRoutes", true)
        next({ ...to, replace: true })
    } else {
        next()
    }
})
async function loadMenu(user) {
    try {
        const res = await Vue.prototype.$axios.post("http://localhost:8888/menu/nav", user, {
            headers: {
                "Content-Type": "application/json"
            }
        })
        console.log(res)
        const menu = res.data.navs;
        const perm = res.data.perm;
        store.commit("setMenuList", menu)
        store.commit("setPermList", perm)
        return menu;
    } catch (e) {
        console.log(e)
        return [];
    }
}
//动态路由
 function addRoutes(menuList) {
    let routes = router.options.routes
    routes.forEach(item => {
        if (item.path === "/") {

            menuList.forEach(menu => {
                if (menu.type != 0) {
                    let childRoute = {
                        path: "/" + menu.path,
                        name: menu.name,
                        meta: { title: menu.name },
                        component: () => import(`@/views/${menu.component}`)
                    }
                    item.children.push(childRoute)
                }
                if (menu.children) {
                    menu.children.forEach(child => {
                        let childRoute = {
                            path: "/" + child.path,
                            name: child.name,
                            meta: { title: child.name },
                            component: () => import(`@/views/${child.component}`)
                        }
                        item.children.push(childRoute)
                    })
                }
            })

        }
    })
    router.addRoutes(routes)
}
// 修改 router.push 和 router.replace 方法，增加错误处理
const originalPush = router.push;
router.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch((err) => err);
};

const originalReplace = router.replace;
router.replace = function replace(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch((err) => err);
};
export default router